package cn.com.api.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiU_dynamic;
import cn.com.api.entity.ApiU_dynamic_reply;
import cn.com.api.entity.ApiU_dynamic_source;
import cn.com.api.entity.ApiU_user_friend_setting;
import cn.com.api.entity.ApiU_zan;

import cn.com.api.mapper.ApiU_dynamicMapper;
import cn.com.api.mapper.ApiU_dynamic_replyMapper;
import cn.com.api.mapper.ApiU_dynamic_sourceMapper;
import cn.com.api.mapper.ApiU_user_friend_settingMapper;
import cn.com.api.mapper.ApiU_zanMapper;

import cn.com.api.service.ApiU_dynamicService;
import cn.com.api.service.ApiU_zanService;
import cn.com.api.service.impl.base.U_dynamicServiceImplBase;
import cn.com.util.PageInfo;
import cn.com.util.Utils;

/**
 * 
 **/
@Service
public class ApiU_dynamicServiceImpl extends U_dynamicServiceImplBase implements ApiU_dynamicService {
	@Autowired
	private ApiU_dynamicMapper apiU_dynamicMapper;
	@Autowired
	private ApiU_dynamic_sourceMapper apiU_dynamic_sourceMapper;
	@Autowired
	private ApiU_dynamic_replyMapper apiU_dynamic_replyMapper;
	@Autowired
	private ApiU_zanMapper apiU_zanMapper;
	@Autowired
	private ApiU_user_friend_settingMapper apiU_user_friend_settingMapper;

	// 新增对象后返回记录ID
	@Override
	public int U_dynamicInsertion(ApiU_dynamic u_dynamic) {
		return this.apiU_dynamicMapper.U_dynamicInsertion(u_dynamic);
	}

	// 查询信息By user_id(我发的动态，不包含回复内容)

	public Map<String, Object> findDynamicInfoByUser_id(String user_id, String touser_id, PageInfo page)
			throws Exception {
		Map<String, Object> result = new HashMap<String, Object>();
		Boolean flag = true;
		ApiU_user_friend_setting friendSetting = apiU_user_friend_settingMapper.getFriendSetting(touser_id, user_id);
		if (friendSetting != null) {
			if (friendSetting.getIs_readdynamic() == 1) {// 可以查看好友动态
				flag = false;
			}
		}

		if (flag) {
			Map<String, Object> param = new HashMap<String, Object>();
			param.put("user_id", touser_id);
			param.put("type", "0");
			List<ApiU_dynamic> dynamicList = apiU_dynamicMapper.findU_dynamicsByMapListPage(param, page);
			for (ApiU_dynamic dynamic : dynamicList) {
				// 动态图片/视频
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("dynamic_id", dynamic.getId());
				List<ApiU_dynamic_source> dynamic_sourceList = this.apiU_dynamic_sourceMapper
						.findU_dynamic_sourcesByMap(map);
				// 缩略图
				String resource_url = "";
				Integer dy_type = dynamic.getContent_type();
				for (ApiU_dynamic_source source : dynamic_sourceList) {
					if (dynamic_sourceList.size() > 1) {
						if (dy_type == 0) // 图片
						{
							resource_url = source.getSource() + "?imageView2/1/w/100/h/100";
							source.setSource(resource_url);
						} else if (dy_type == 1) {// 视频
							resource_url = source.getSource() + "?vframe/png/offset/1/w/575/h/310";
							source.setSource(resource_url);
						}
					} else {
						if (dy_type == 0) // 图片
						{
							resource_url = source.getSource() + "?imageView2/1/w/575/h/310";
							source.setSource(resource_url);
						} else if (dy_type == 1) {// 视频
							resource_url = source.getSource() + "?vframe/png/offset/1/w/575/h/310";
							source.setSource(resource_url);
						}
					}
				}
				dynamic.setDynamic_source_list(dynamic_sourceList);
				// //回复信息
				// List<ApiU_dynamic_reply> dynamic_replyList =
				// this.apiU_dynamic_replyMapper.findU_dynamic_replysByMap(map);
				// dynamic.setDynamic_reply_list(dynamic_replyList);
				// 点赞列表
				List<ApiU_zan> zan_list = this.apiU_zanMapper.findU_dynamicsZanByConnID(dynamic.getId());
				dynamic.setZan_list(zan_list);
			}
			result.put(Utils.DATAS, dynamicList);
			result.put(Utils.RESULT, Utils.RESULT_RC_SUCCESS);
			result.put(Utils.MESSAGE, "查询成功！");
		} else {
			result.put(Utils.RESULT, Utils.RESULT_RC_CHECK_FAIL);
			result.put(Utils.MESSAGE, "你无法查看好友动态");
		}
		return result;
	}

	public List<ApiU_dynamic> findDynamicInfoByUser_id(Map param, PageInfo pageInfo) {
		List<ApiU_dynamic> dynamicList = apiU_dynamicMapper.findU_dynamicsByMapListPage(param, pageInfo);
		for (ApiU_dynamic dynamic : dynamicList) {
			// 动态图片/视频
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("dynamic_id", dynamic.getId());
			List<ApiU_dynamic_source> dynamic_sourceList = this.apiU_dynamic_sourceMapper
					.findU_dynamic_sourcesByMap(map);
			// 缩略图
			String resource_url = "";
			Integer dy_type = dynamic.getContent_type();
			for (ApiU_dynamic_source source : dynamic_sourceList) {
				if (dynamic_sourceList.size() > 1) {
					if (dy_type == 0) // 图片
					{
						resource_url = source.getSource() + "?imageView2/1/w/100/h/100";
						source.setSource(resource_url);
					} else if (dy_type == 1) {// 视频
						resource_url = source.getSource() + "?vframe/png/offset/1/w/575/h/310";
						source.setSource(resource_url);
					}
				} else {
					if (dy_type == 0) // 图片
					{
						resource_url = source.getSource() + "?imageView2/1/w/575/h/310";
						source.setSource(resource_url);
					} else if (dy_type == 1) {// 视频
						resource_url = source.getSource() + "?vframe/png/offset/1/w/575/h/310";
						source.setSource(resource_url);
					}
				}
			}

			dynamic.setDynamic_source_list(dynamic_sourceList);
			// //回复信息
			// List<ApiU_dynamic_reply> dynamic_replyList =
			// this.apiU_dynamic_replyMapper.findU_dynamic_replysByMap(map);
			// dynamic.setDynamic_reply_list(dynamic_replyList);
			// 点赞列表
			List<ApiU_zan> zan_list = this.apiU_zanMapper.findU_dynamicsZanByConnID(dynamic.getId());
			dynamic.setZan_list(zan_list);
		}

		return dynamicList;
	}

	// 查询信息(我的朋友圈，包含回复内容)

	public List<ApiU_dynamic> findDynamicInfo(String user_id, PageInfo pageInfo) {
		List<ApiU_dynamic> dynamicList = apiU_dynamicMapper.findU_dynamicsInfoByUserIDListPage(user_id, pageInfo);

		for (ApiU_dynamic dynamic : dynamicList) {
			// 动态图片/视频
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("dynamic_id", dynamic.getId());

			List<ApiU_dynamic_source> dynamic_sourceList = this.apiU_dynamic_sourceMapper
					.findU_dynamic_sourcesByMap(map);
			// 缩略图
			String resource_url = "";
			Integer dy_type = dynamic.getContent_type();
			for (ApiU_dynamic_source source : dynamic_sourceList) {
				if (dynamic_sourceList.size() > 1) {
					if (dy_type == 0) // 图片
					{
						resource_url = source.getSource() + "?imageView2/1/w/100/h/100";
						source.setSource(resource_url);
					} else if (dy_type == 1) {// 视频
						resource_url = source.getSource() + "?vframe/png/offset/1/w/575/h/310";
						source.setSource(resource_url);
					}
				} else {
					if (dy_type == 0) // 图片
					{
						resource_url = source.getSource() + "?imageView2/1/w/575/h/310";
						source.setSource(resource_url);
					} else if (dy_type == 1) {// 视频
						resource_url = source.getSource() + "?vframe/png/offset/1/w/575/h/310";
						source.setSource(resource_url);
					}
				}
			}

			dynamic.setDynamic_source_list(dynamic_sourceList);
			// 回复信息
			List<ApiU_dynamic_reply> dynamic_replyList = this.apiU_dynamic_replyMapper.findU_dynamic_replysByMap(map);
			dynamic.setDynamic_reply_list(dynamic_replyList);
			// 点赞列表
			List<ApiU_zan> zan_list = this.apiU_zanMapper.findU_dynamicsZanByConnID(dynamic.getId());
			dynamic.setZan_list(zan_list);
		}

		return dynamicList;
	}

	// 回复动态

	public Integer reply_Dynamic(ApiU_dynamic_reply dynamic_reply) {
		ApiU_dynamic dynamic = new ApiU_dynamic();
		return this.apiU_dynamic_replyMapper.U_dynamic_replyInsertion(dynamic_reply);
	}
}
